package com.cskaoyan.service.mallservice;

import com.cskaoyan.bean.mallbean.Storage;
import com.cskaoyan.mapper.mallmapper.StorageMapper;
import com.cskaoyan.vo.mallvo.StorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author hsp
 * @date 2021/5/9 20:13
 */
@Service
public class ImageServiceImpl implements ImageService {

//    @Value("${spring.mvc.staticPathPattern}")
//    String url;// /wx/storage/fetch/**

    @Value("${baseUrl}")  //http://localhost:3306
    String baseUrl;
    @Autowired
    StorageMapper storageMapper;
    @Override
    public StorageVo createStorage(MultipartFile file) {
        //文件大小
        long size = file.getSize();
        //文件type类型 -->image/jpeg
        String contentType = file.getContentType();
        //上传时的文件名 --> MatePadPro.jpg
        String originalFilename = file.getOriginalFilename();
        int indexDot = originalFilename.indexOf(".");
        //获得.jpg .png .gif等
        String fileSuffix = originalFilename.substring(indexDot);
        originalFilename = UUID.randomUUID().toString() + originalFilename;
        String localFilename ="";

        for (int i = 0; i < originalFilename.length(); i++) {

            int a = (int) originalFilename.charAt(i);
            String s = Integer.toHexString(a);
            localFilename = localFilename + s; //将文件名转化为16进制的字符串
        }
        localFilename = localFilename.substring(0, 20) + fileSuffix;
        //请求时的形参名 name="file"
//        String name = file.getName();


//        获取相对路径 D:\workspace\ideaProjects\git\project2\mall29
        String dir = System.getProperty("user.dir");
        String  path = dir+"/"+"src/main/resources/images"+"/"+localFilename;
        File localFile = new File(path);
//        File localFile = new File("src/main/resources/images"+"/"+originalFilename);
        try {
            file.transferTo(localFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Storage storage = new Storage();
        storage.setId(null);
        storage.setKey(localFilename);
        storage.setName(file.getOriginalFilename());
        storage.setType(contentType);
        storage.setSize((int) size);
        storage.setUrl("/wx/storage/fetch/"+localFilename);
        // todo addTime应是不变的
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setDeleted(false);

        storageMapper.insert(storage);


        StorageVo storageVo = new StorageVo();

        storageVo.setAddTime(new SimpleDateFormat().format(storage.getAddTime()));
        storageVo.setId(storage.getId());
        storageVo.setKey(storage.getKey());
        storageVo.setName(storage.getName());
        storageVo.setSize(storage.getSize());
        storageVo.setType(storage.getType());
        storageVo.setUpdateTime(new SimpleDateFormat().format(storage.getUpdateTime()));
        storageVo.setUrl(baseUrl+storage.getUrl());
        return storageVo;
    }
}
